local LuaMongo = _G.lua_mongo
local DB = require("common.DB")
local RankDefine = require("rank.RankDefine")
local Msg = require("core.Msg")
local PetConfig = require("excel.Pet").pet
local Log = require("common.Log")
local PfLogic = require("tencentApi.PfLogic")

PET_RANK = PET_RANK or {}
--PET_RANK = {index = {uuid, name, petName, petStage, petPower, create = os.time()}}

function setRankList(human, list, index)
  list.type = RankDefine.PET_RANK_TYPE
  list.name = PET_RANK[index].name
  list.uuid = PET_RANK[index].uuid
  list.petName = PET_RANK[index].petName
  list.petStage = PET_RANK[index].petStage
  list.petPower = PET_RANK[index].petPower
  if PfLogic.isBlueShow(human) then
    list.blue = PET_RANK[index].blue or "0000"
  else
    list.blue = "0000"
  end
  list.args = 0
end

function GetRankList(human, sendMsg)
  sendMsg.petlist[0] = #PET_RANK
  for i=1, #PET_RANK do
    if PET_RANK[i].uuid == human._id then
      sendMsg.myRank = i
    end
    setRankList(human, sendMsg.petlist[i], i)
  end 
  Msg.send(sendMsg, human.fd)
end

function getFirst(human, list)  
  list[0] = 0 
  if PET_RANK[1] then
    list[0] = list[0] + 1
    setRankList(human, list[list[0]], 1)
  end
end

function getPetinfo(human, power)
  local tmp = {}
  local stage = 0
  for i = 0, human.db.petLen -1 do
    if stage < human.db.pet[i].stage then
      tmp.petStage = human.db.pet[i].stage
      tmp.petName = PetConfig[human.db.pet[i].id].petMingzi
      stage = human.db.pet[i].stage
    end
  end
  tmp.petPower = power
  return tmp
end

function setRankInfo(human, index, petInfo)
  PET_RANK[index] = {}
  PET_RANK[index].uuid = human._id  
  PET_RANK[index].petName = petInfo.petName
  PET_RANK[index].name = human.name
  PET_RANK[index].petStage = petInfo.petStage
  PET_RANK[index].petPower = petInfo.petPower
  PET_RANK[index].account = human.account
  PET_RANK[index].create = os.time()
  PET_RANK[index].blue = string.format("%d%d%d%d", human.db.blueVipLv, human.db.blueYearVip, human.db.blueSuperVip, human.db.level3366)
end

function sortFunc(a, b)
  if a.petPower == b.petPower then
    return a.create < b.create
  end
  return a.petPower > b.petPower
end

function isInRank(human, petInfo)
  for i=1, #PET_RANK do
    if PET_RANK[i].uuid == human._id then
      if petInfo.petPower ~= PET_RANK[i].petPower then
        setRankInfo(human, i, petInfo)
        table.sort(PET_RANK, sortFunc)
      end
      return true
    end
  end
  return false
end

--外部调用，神将战力变更的时候调用
function petPowerChange(human, power)
  if human.db.lv < 35 then return end
  local petInfo = getPetinfo(human, power)
  if isInRank(human, petInfo) then
    return
  end
  if #PET_RANK < RankDefine.RANK_MAX_CNT then
    setRankInfo(human, #PET_RANK + 1, petInfo)
  elseif #PET_RANK >= RankDefine.RANK_MAX_CNT and power > PET_RANK[#PET_RANK].petPower then
    setRankInfo(human, RankDefine.RANK_MAX_CNT, petInfo)
  else
    return false
  end
  table.sort(PET_RANK, sortFunc)
end

function getRank()
  local dbType = {type = RankDefine.PET_RANK_TYPE}
  LuaMongo.find(DB.db_rank, dbType)
  while true do
    local tmp = {}
    if not LuaMongo.next(tmp) then
      break
    end
    PET_RANK[tmp.index] = {}
    PET_RANK[tmp.index].uuid = tmp.uuid
    PET_RANK[tmp.index].name = tmp.name
    PET_RANK[tmp.index].petStage = tmp.petStage
    PET_RANK[tmp.index].petName = tmp.petName
    PET_RANK[tmp.index].petPower = tmp.petPower
    PET_RANK[tmp.index].account = tmp.account
    PET_RANK[tmp.index].create = tmp.create or os.time()
    PET_RANK[tmp.index].blue = tmp.blue or "0000"
  end
end

function wrapperPetData(index)
  local db = {}
  db.type = RankDefine.PET_RANK_TYPE
  db.index = index
  db.uuid = PET_RANK[index].uuid
  db.name = PET_RANK[index].name
  db.petName = PET_RANK[index].petName
  db.petPower = PET_RANK[index].petPower
  db.petStage = PET_RANK[index].petStage
  db.account = PET_RANK[index].account
  db.create = PET_RANK[index].create or os.time()
  db.blue = PET_RANK[index].blue or "0000"
  return db
end

function saveRank()
  for i=1, #PET_RANK do
    local db = wrapperPetData(i)
    LuaMongo.insert(DB.db_rank, db)
  end
end

function writeLog()
  for i=1, #PET_RANK do
    Log.write(Log.LOGID_OSS_PET_RANK, PET_RANK[i].uuid, PET_RANK[i].account, PET_RANK[i].name, PET_RANK[i].petPower, i)
  end
end